package com.auto.autoapp.util

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.auto.autoapp.AutoApplication
import com.auto.autoapp.data.model.UserInfo
import com.auto.autoapp.model.AccountRaiseSetting
import com.auto.autoapp.model.CommentPrivateMessageSetting
import com.auto.autoapp.model.CommonSetting
import com.auto.autoapp.model.LiveMemberPrivateMsgSetting
import com.auto.autoapp.model.UpVideoListenSetting
import com.auto.autoapp.model.WatchVideoCommentSetting
import com.google.gson.Gson

object SpUtil {
    private const val KEY_TOKEN = "token"
    private const val KEY_USER_ACCOUNT = "user_account"


    private const val KEY_AR_SETTINGS_JSON = "ar_settings_json"

    // Aggregated JSON object storage for other settings
    private const val KEY_PM_SETTINGS_JSON = "pm_settings_json"
    private const val KEY_VC_SETTINGS_JSON = "vc_settings_json"
    private const val KEY_LIVE_SETTINGS_JSON = "live_settings_json"
    private const val KEY_UP_SETTINGS_JSON = "up_settings_json"
    private const val KEY_COMMON_SETTING_JSON = "common_setting_json"

    private const val KEY_USER_IFO = "user_info"

    private val gson = Gson()

    private val prefs: SharedPreferences by lazy {
        AutoApplication.instance.getSharedPreferences("sp", Context.MODE_PRIVATE)
    }

    fun getSp(): SharedPreferences = prefs

    fun setUserInfo(userInfo: UserInfo) {
        prefs.edit { putString(KEY_USER_IFO, gson.toJson(userInfo)) }
    }

    fun getUserInfo(): UserInfo? {
        val json = prefs.getString(KEY_USER_IFO, null)
        if (json == null) null
        return gson.fromJson(json, UserInfo::class.java)

    }

    fun setToken(token: String) {
        prefs.edit { putString(KEY_TOKEN, token) }
    }

    fun getToken(): String? = prefs.getString(KEY_TOKEN, null)

    fun setUserAccount(account: String) {
        prefs.edit { putString(KEY_USER_ACCOUNT, account) }
    }

    fun getUserAccount(): String? = prefs.getString(KEY_USER_ACCOUNT, null)
    fun clear() {
        prefs.edit(commit = true) { clear() }
    }


    /**
     * 设置获客配置
     */
    fun setAccountRaiseSetting(accountRaiseSetting: AccountRaiseSetting) {
        prefs.edit { putString(KEY_AR_SETTINGS_JSON, gson.toJson(accountRaiseSetting)) }
    }

    /**
     * 获取获客配置
     */
    fun getAccountRaiseSetting(): AccountRaiseSetting {
        val json = prefs.getString(KEY_AR_SETTINGS_JSON, null)
        if (json == null) return AccountRaiseSetting()
        return gson.fromJson<AccountRaiseSetting>(json, AccountRaiseSetting::class.java)
    }


    /***
     * 设置视频监听并评论
     */
    fun setUpSettings(setting: UpVideoListenSetting) {
        prefs.edit { putString(KEY_UP_SETTINGS_JSON, gson.toJson(setting)) }
    }

    /**
     * 获取视频监听并评论设置
     */
    fun getUpSettings(): UpVideoListenSetting {
        val json = prefs.getString(KEY_UP_SETTINGS_JSON, null)
        if (json == null) return UpVideoListenSetting()
        return gson.fromJson<UpVideoListenSetting>(json, UpVideoListenSetting::class.java)
    }


    /**
     * 监听视频评论设置
     */
    fun setWatchVideoCommentSetting(setting: WatchVideoCommentSetting) {
        prefs.edit { putString(KEY_VC_SETTINGS_JSON, gson.toJson(setting)) }
    }

    fun getWatchVideoCommentSetting(): WatchVideoCommentSetting {
        val json = prefs.getString(KEY_VC_SETTINGS_JSON, null)
        if (json == null) return WatchVideoCommentSetting()
        return gson.fromJson<WatchVideoCommentSetting>(json, WatchVideoCommentSetting::class.java)

    }

    fun setLivePMSetting(setting: LiveMemberPrivateMsgSetting) {
        prefs.edit { putString(KEY_LIVE_SETTINGS_JSON, gson.toJson(setting)) }
    }

    fun getLivePMSetting(): LiveMemberPrivateMsgSetting {
        val json = prefs.getString(KEY_LIVE_SETTINGS_JSON, null)
        if (json == null) return LiveMemberPrivateMsgSetting()
        return gson.fromJson<LiveMemberPrivateMsgSetting>(
            json,
            LiveMemberPrivateMsgSetting::class.java
        )

    }


    fun setWatchVideoCommentSetting(setting: CommentPrivateMessageSetting) {
        prefs.edit { putString(KEY_VC_SETTINGS_JSON, gson.toJson(setting)) }
    }

//    fun getWatchVideoCommentSetting(): CommentPrivateMessageSetting {
//        val json = prefs.getString(KEY_VC_SETTINGS_JSON, null)
//        if (json == null) return CommentPrivateMessageSetting()
//        return gson.fromJson<CommentPrivateMessageSetting>(
//            json,
//            CommentPrivateMessageSetting::class.java
//        )
//
//    }


    // Aggregated JSON storage for PM settings
    fun setPmSettingsJson(json: String) {
        prefs.edit { putString(KEY_PM_SETTINGS_JSON, json) }
    }

    fun getPmSettingsJson(): String? = prefs.getString(KEY_PM_SETTINGS_JSON, null)


    fun setCommentPrivateMsgSetting(setting: CommentPrivateMessageSetting) {
        prefs.edit { putString(KEY_PM_SETTINGS_JSON, gson.toJson(setting)) }
    }

    fun getCommentPrivateMsgSetting(): CommentPrivateMessageSetting {
        val json = prefs.getString(KEY_PM_SETTINGS_JSON, null)
        if (json == null) return CommentPrivateMessageSetting()
        return gson.fromJson<CommentPrivateMessageSetting>(
            json,
            CommentPrivateMessageSetting::class.java
        )

    }

    fun setCommonSetting(setting: CommonSetting) {
        prefs.edit { putString(KEY_COMMON_SETTING_JSON, gson.toJson(setting)) }
    }

    fun getCommonSetting(): CommonSetting {
        val json = prefs.getString(KEY_COMMON_SETTING_JSON, null)
        if (json.isNullOrEmpty()) return CommonSetting()
        return gson.fromJson<CommonSetting>(json, CommonSetting::class.java)
    }


}